//
//  SucceedViewController.m
//  SecretPhoto
//
//  Created by administrator on 12-9-14.
//  Copyright (c) 2012年 com.ptc. All rights reserved.
//

#import "SucceedViewController.h"
#import "TableViewController.h"

//#import "TableViewController.h"

@interface SucceedViewController ()

@end

@implementation SucceedViewController

@synthesize imageView;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
   
    
    
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{

    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


///创建文件夹
- (IBAction)create:(id)sender {
    
    NSString *imageDir = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), @"Image"];
    BOOL isDir = NO;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL existed = [fileManager fileExistsAtPath:imageDir isDirectory:&isDir];
    
    
    //    UIAlertView* choice=[[UIAlertView alloc]init];
    //
    //    choice=[[UIAlertView alloc] initWithTitle:@"消息" message:@"创建相册" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: @"取消",nil];
    //
    //    [choice show];
    
    
    
    if ( !(isDir == YES && existed == YES) )
    {
        [fileManager createDirectoryAtPath:imageDir withIntermediateDirectories:YES attributes:nil error:nil];
        
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"创建成功！"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"相册已存在！"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        
        
        
        
    }
    
}







///删除文件夹及文件级内的文件
- (IBAction)dele:(id)sender {
    
    NSString *imageDir = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), @"Image"];
    BOOL isDir = NO;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL existed = [fileManager fileExistsAtPath:imageDir isDirectory:&isDir];
    
    if ( !(isDir == YES && existed == YES) )
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"不存在相册！"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        
    }
    else
    {
        [fileManager removeItemAtPath:imageDir error:nil];
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"删除成功！"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    
    
}



///
- (IBAction)takePicture:(id)sender {
    
    
    NSString *imageDir = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), @"Image"];
    BOOL isDir = NO;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL existed = [fileManager fileExistsAtPath:imageDir isDirectory:&isDir];
    if ( !(isDir == YES && existed == YES) )
    {
        [fileManager createDirectoryAtPath:imageDir withIntermediateDirectories:YES attributes:nil error:nil];
        
        
        //        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
        //                                                        message:@"创建成功！"
        //                                                       delegate:self
        //                                              cancelButtonTitle:@"确定"
        //                                              otherButtonTitles:nil];
        //        [alert show];
        //        [alert release];
        
    }
    
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        NSArray *temp_MediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
        picker.mediaTypes = temp_MediaTypes;
        picker.delegate = self;
        picker.allowsImageEditing = YES;
    }
    
    [self presentModalViewController:picker animated:YES];
    [picker release];
    
    //    NSDateFormatter *time = [[[NSDateFormatter alloc] init ]autorelease];
    //    [time setDateFormat:@"yyyyMMddHHmmssSS"];
    //    NSString *timenow = [time stringFromDate:[NSDate date]];
    //    NSString *imagename=[NSString stringWithFormat:@"Image/%@.jpg",timenow];
    //    NSLog(@"%@",imagename);
    
    
    
}




/*下面两个函数是遵守 UIImagePickerControllerDelegate这个协议所实现的类.这样就能够完整的实现,获取照片或者视频,然后写入文件的过程.*/

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    
    NSDateFormatter *time = [[[NSDateFormatter alloc] init ]autorelease];  ///以时间命名文件
    [time setDateFormat:@"yyyyMMddHHmmssSS"];
    NSString *timenow = [time stringFromDate:[NSDate date]];
    NSString *imagename=[NSString stringWithFormat:@"Image/%@.jpg",timenow];
    
    
    
    
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    if ([mediaType isEqualToString:@"public.image"]){
        
        UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
        NSLog(@"found an image");
        
        //   NSString *imageFile = [documentsDirectory stringByAppendingPathComponent:@"Image/temp.jpg"];
        NSString *imageFile = [documentsDirectory stringByAppendingPathComponent:imagename];
        
        NSLog(@"%@", imageFile);
        
        success = [fileManager fileExistsAtPath:imageFile];
        if(success) {
            success = [fileManager removeItemAtPath:imageFile error:&error];
        }
        
        imageView.image = image;
        
        [UIImageJPEGRepresentation(image, 1.0f) writeToFile:imageFile atomically:YES];
        
        //SETIMAGE(image);
        //CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);
    }
    else if([mediaType isEqualToString:@"public.movie"]){
        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
        NSLog(@"%@", videoURL);
        NSLog(@"found a video");
        NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
        
        /****************************************/
        
        NSString *videoFile = [documentsDirectory stringByAppendingPathComponent:@"temp.mov"];
        NSLog(@"%@", videoFile);
        
        success = [fileManager fileExistsAtPath:videoFile];
        if(success) {
            success = [fileManager removeItemAtPath:videoFile error:&error];
        }
        [videoData writeToFile:videoFile atomically:YES];
        //CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);
        //NSLog(videoURL);
    }
    [picker dismissModalViewControllerAnimated:YES];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
    [picker dismissModalViewControllerAnimated:YES];
    
}


- (void)dealloc {
    [imageView release];
    [super dealloc];
}



- (IBAction)look:(id)sender {
    TableViewController *tableView = [[TableViewController alloc]init];   //界面跳转
    tableView.title=@"照片";
    [self.navigationController pushViewController:tableView animated:YES];
    
//    TableViewController *tableViewController =[[[TableViewController alloc]initWithNibName:@"tableViewController" bundle:nil]autorelease];
//    [self.view.window setRootViewController:tableViewController];
    
    
}
@end
